
import random  # 取随机数
import datetime  # 取随机数
import time  # 取随机数
import hashlib  # 取随机数
import json  # 取随机数
import requests  # 取随机数

class SendCodeViewSet():
    def post(self, phone):
        url = "https://api.netease.im/sms/sendcode.action"
        data = {
            'mobile': phone,
            # 'mobile': '13808837054',  刘总
            # 'mobile': '13622327097',  家伟
            'templateid': 14843254
        }
        # 哈希加密
        header = self.get_header()
        print(header)
        # data = {'mobile': str(mobile), 'codeLen': codeLen}
        response = requests.post(url=url, data=data, headers=header)
        print()
        text = json.loads(response.text)
        print(text)
        # return Response({'status': text['code']})

    def get_header(self):
        appkey = 'e056fea01c1c365748929e83e715715e'
        appsecret = '58e56012362b'
        nonce = random.randint(10000, 100000000)
        ctime = datetime.datetime.utcnow()
        curtime = str(int(time.mktime(ctime.timetuple())))
        s = appsecret + str(nonce) + curtime
        checksum = hashlib.sha1(s.encode('utf-8')).hexdigest()
        Content_Type = "application/x-www-form-urlencoded;charset=utf-8"
        header = {'Content-Type': Content_Type, 'AppKey': appkey, 'Nonce': str(nonce), 'CurTime': curtime,'CheckSum': checksum}
        return header


if __name__ == '__main__':
    SendCodeViewSet().post('13622327097')
